﻿using B.S.BasicData.Api.Write.Application.Command.GridPerson;
using B.S.BasicData.Domain.Entities;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;

namespace B.S.BasicData.Api.Write.Application.CommandHandler.GridPerson
{
    /// <summary>
    /// 删除网格人员的处理类
    /// </summary>
    public class DelGridPersonHandler:IRequestHandler<DelGridPersonCommand,ApiResult<int>>
    {
        private readonly IBaseRepository<AccountModel> accRepo;

        /// <summary>
        /// 通过构造函数注入需要的资源
        /// </summary>
        /// <param name="accRepo">账号仓储</param>
        public DelGridPersonHandler(IBaseRepository<AccountModel> accRepo) {
            this.accRepo = accRepo;
        }
        /// <summary>
        ///  删除网格人员的处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<int>> Handle(DelGridPersonCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();

            var model = accRepo.GetModel(request.Id);
            if (model == null) {
                result.Code = ApiEnums.Error;
                result.Msg = "您要删除的账号不存在!";
                result.Data = 0;
                return Task.FromResult(result);
            }

            model.IsDeleted = true;

            var r = accRepo.Update(model);
             

            if (r > 0)
            {
                result.Code = ApiEnums.Success;
                result.Msg = "删除账号成功!";
                result.Data = r;

            }
            else
            {
                result.Code = ApiEnums.Fail;
                result.Msg = "删除账号失败!";
                result.Data = r;
            }


            return Task.FromResult(result);
        }
    }
}
